<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
class Project extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form', 'url'));
	}

	function projectsList($id_project=null)
	{
		//die('asshole');
		
		if($this->checkLogin() == true)
		{
			if($id_project == "")
			{
			//die("null");
			$data['username'] = $this->session->userdata('logged_in');
			$data['level'] = $this->session->userdata('level');
			$data['threads'] = $this->Project_model->getParentProject();
			$this->load->view('projects/indexProject', $data);
			}
			else
			{
			die("not null");
			$data['username'] = $this->session->userdata('logged_in');
			$data['level'] = $this->session->userdata('level');
			$data['threads'] = $this->Project_model->get($id_project);
			$data['projectId'] = $id_project;
			$this->load->view('projects/indexProject', $data);
			}
		}
		else
		{
			redirect('login', 'refresh');
		}
	}
	
	function ThreadList($id_project)
	{
		if($this->checkLogin() == true)
		{
			$data['username'] = $this->session->userdata('logged_in');
			$data['idProject'] = $id_project;
			$data['level'] = $this->session->userdata('level');
			$data['threads'] = $this->Project_model->get($id_project);
			$data['approvals'] = $this->Project_model->getApproval();
			$data['is_having_ig'] = $this->Project_model->is_having_ig($id_project);
			//$data['row_approvedByKonsultan'] = $this->Project_model->checkROWByKonsultan($id_project);
			$data['projectId'] = $id_project;
			$this->load->view('projects/indexThread', $data);
		}
		else
		{
			redirect('login');
		}
	}
	
	function listROW($idThread)
	{
		if($this->checkLogin() == true)
		{
			$data['username'] = $this->session->userdata('logged_in');
			$data['idThread'] = $idThread;
			$data['level'] = $this->session->userdata('level');
			$data['threads'] = $this->Project_model->get($idThread);
			$data['approvals'] = $this->Project_model->getApproval($idThread);
			$data['request_of_works'] = $this->Project_model->getROW($idThread);
			//$data['row_approvedByKonsultan'] = $this->Project_model->checkROWByKonsultan($id_project);
			$this->load->view('projects/listROW', $data);
		}
		else
		{
			redirect('login');
		}
	}
	
	function showROWDetails($idROW)
	{
		if($this->checkLogin() == true)
		{
			$data['username'] = $this->session->userdata('logged_in');
			$data['level'] = $this->session->userdata('level');
			$data['idROW'] = $idROW;
			$data['request_of_works'] = $this->Project_model->getROWById($idROW);
			$data['row_details'] = $this->Project_model->getROWDetail($idROW);
			$data['row_files'] = $this->Project_model->getROWFiles($idROW);
			//$data['row_approvedByKonsultan'] = $this->Project_model->checkROWByKonsultan($id_project);
			$this->load->view('projects/showROW', $data);
		}
		else
		{
			redirect('login');
		}
	}
	
	function ShowApproval($idThread)
	{
		if($this->checkLogin() == true)
		{
		//die($idProject);
		
			$data['username'] = $this->session->userdata('logged_in');
			$data['level'] = $this->session->userdata('level');
			$data['thread'] = $this->Project_model->getThread($idThread);
			$data['approvals'] = $this->Project_model->getApproval($idThread);
			$data['files'] = $this->Project_model->getFiles();
			$data['projectId'] = $idThread;
			$this->load->view('projects/showApproval', $data);
		}
		else
		{
			redirect('login');
		}
	}
	
	function addApproval($idThread)
	{
		if($this->checkLogin() == true)
		{
			$data['username'] = $this->session->userdata('logged_in');
			$data['level'] = $this->session->userdata('level');
			$data['thread'] = $this->Project_model->getThread($idThread);
			$data['projectId'] = $idThread;
			$this->load->view('projects/addApproval', $data);
		}
		else
		{
			redirect('login');
		}
	}
	
	function addIG($idThread)
	{
		if($this->checkLogin() == true)
		{
			//die($idThread);
			$data['username'] = $this->session->userdata('logged_in');
			$data['level'] = $this->session->userdata('level');
			$data['thread'] = $this->Project_model->getThread($idThread);
			$data['projectId'] = $idThread;
			$this->load->view('projects/formIG', $data);
		}
		else
		{
			redirect('login');
		}
	}
	
	function saveIG()
	{
		if($this->checkLogin() == true)
		{
			$idThread=$this->input->post('idThread');
			$subject=$this->input->post('subject');
			$drawing_title=$this->input->post('drawing_title');
			$ref_no=$this->input->post('ref_no');
			$drawing_no=$this->input->post('drawing_no');
			$location=$this->input->post('location');
			$data = array(
			'id_sd_thread'=>$idThread,
			'subject'=>$subject,
			'drawing_title'=>$drawing_title,
			'ref_no'=>$ref_no,
			'drawing_no'=>$drawing_no,
			'location'=>$location,
			'username'=>$this->session->userdata('logged_in'),
			'tanggal'=>date('Y-m-d h:i:s')
			);
			$idIG= $this->Project_model->insertIG($data);
			$level1 = $this->input->post('level1');
			$level2 = $this->input->post('level2');
			$level3 = $this->input->post('level3');
		
		foreach($level1 as $key => $value):
			$data1 = array('desc'=>$value,
				      'id_ig'=>$idIG,
				      'parent'=>0);
			$id = $this->Project_model->insert_question($data1);	
			if(isset($level2[$key])){
				if(count($level2[$key])!=0){
					foreach($level2[$key] as $key2 => $value2):
						
						$data2 = array('desc'=>$value2,
							      'id_ig'=>$idIG,
							      'parent'=>$id);
						$id2 = $this->Project_model->insert_question($data2);
						
						if(isset($level3[$key2])){
							if(count($level3[$key2])!=0){
								foreach($level3[$key2] as $key3 => $value3):
					
									$data3 = array('desc'=>$value3,
										      'id_ig'=>$idIG,
										      'parent'=>$id2);
									$this->Project_model->insert_question($data3);
						
								endforeach;
							}
						
						}
					endforeach;
				}
			}
		endforeach;
		//die();
			$data['inspection_guide'] = $this->Project_model->getIG($idThread);
			$data['list_question'] = $this->Project_model->getQuestion($idThread);
			$data['childs'] = $this->Project_model->getChildQuestion();
			//$this->load->view('projects/printIG', $data);
			redirect('project/showThread/'.$idThread);
		}
		else
		{
			redirect('login');
		}
	}
	
	function formROW($idThread, $idProject)
	{
		//die($idThread.':test:'.$idProject);
		//die($this->session->userdata('level'));
		if($this->checkLogin() == true AND $this->session->userdata('level') == 2)
		{
			$data['username'] = $this->session->userdata('logged_in');
			$data['level'] = $this->session->userdata('level');
			$data['thread'] = $this->Project_model->getThread($idThread);
			$data['projectId'] = $idThread;
			$data['idProject'] = $idProject;
			$this->load->view('projects/addROW', $data);
		}
		else
		{
			redirect('login');
		}
	}
	
	function saveROW()
	{
		$idThread = $this->input->post('id_thread');
		$kepada = $this->input->post('kepada');
		$subject = $this->input->post('subject');
		$no_row = $this->input->post('no_row');
		$idProject = $this->input->post('idProject');
		$date = $this->input->post('date');
		$no_pay_item = $this->input->post('no_pay_item');
		$kind_of_work = $this->input->post('kind_of_work');
		$unit = $this->input->post('unit');
		$quantity = $this->input->post('quantity');
		$location = $this->input->post('location');
		$time_inspection = $this->input->post('time_inspection');
		
		$dataROW=array('id_sd_thread'=> $idThread,
				'username'=> $this->session->userdata('logged_in'),
				'no_row'=> $no_row,
				'kepada'=> $kepada,
				'date_inspection'=> $date,
				'approved_by_konsultan'=> NULL,
				'verifier_konsultan'=> NULL,
				'approved_by_mp'=> NULL,
				'verifier_mp'=> NULL,
				'row_status'=> "NOT REVIEW"
				);
		$idROW = $this->Project_model->insertROW($dataROW);
		$count = count($no_pay_item);
		//insertDetails
		for($i=0;$i<$count;$i++)
		{
			$dataDetail = array(
				'id_row'=> $idROW,
				'no_pay_item'=> $no_pay_item[$i],
				'kind_of_work'=> $kind_of_work[$i],
				'unit'=> $unit[$i],
				'quantity'=> $quantity[$i],
				'location'=> $location[$i],
				'time_inspection'=> $time_inspection[$i]
			);
			$this->Project_model->insertROWDetails($dataDetail);
		}
		$config =  array(
           'upload_path'     => "./assets/file_upload/",
           'allowed_types'   => "gif|jpg|png|jpeg|pdf",
           'overwrite'       => TRUE,
           'max_size'        => "2048000",  // Can be set to particular file size
           'max_height'      => "1111768",
           'max_width'       => "1111024"  
           );    
		$this->load->library('upload', $config);
				if ($this->upload->do_upload('userfile'))
				{
					$data = $this->upload->data();
					$file_path     = $data['file_path'];
					$file         = $data['full_path'];
					$file_ext     = $data['file_ext'];
					$final_file_name = "RequestOfWork_".time().$file_ext;
					$dataFile = array(
					'id_row' => $idThread,
					'nama_file' => $final_file_name
					);
					$this->Project_model->insertROWFiles($dataFile);
				}
				else
				{
					$errors = $this->upload->display_errors();
					var_dump($errors);
				}
		redirect('project/ThreadList/'.$idProject);
	}
	
	function showIG($idThread)
	{
		$data['inspection_guide'] = $this->Project_model->getIG($idThread);
		$data['list_question'] = $this->Project_model->getQuestion($idThread);
		$data['childs'] = $this->Project_model->getChildQuestion();
		$this->load->view('projects/printIG', $data);
	}
	
	function notApprove()
	{
		if($this->checkLogin() == true)
		{
			$idThread = $this->input->post('id_sd_thread');
			$status = $this->input->post('status');
			$komentar = $this->input->post('komentar');
			$username = $this->session->userdata('logged_in');
			if($this->session->userdata('level') == 3)
			{
				$data = array(
				'id_sd_thread'=> $idThread,
				'approved_by_konsultan'=> $status,
				'verifier_konsultan'=> $username,
				'status'=> "Rejected By ".$username,
				'level'=> $this->session->userdata('level')
				);
				$this->Project_model->updateSD($data);
			}
			else
			{
				$data = array(
				'id_sd_thread'=> $idThread,
				'approved_by_mp'=> $status,
				'verifier_mp'=> $username,
				'status'=> "Rejected By ".$username,
				'level'=> $this->session->userdata('level')
				);
				$this->Project_model->updateSD($data);
			}
			$dataPost = array(
			'isi_sd_post'=>$komentar,
			'username'=>$username,
			'tanggal'=>date('Y-m-d h:i:s'),
			'id_sd_thread'=>$idThread
			);
			$this->Project_model->insertPost($dataPost);
			
			//die('notApprove');
			redirect('project/showThread/'.$idThread);
		}
		else
		{
			redirect('login');
		}
	}
	
	function approveSD()
	{
	if($this->checkLogin() == true)
	{
		$idThread = $this->input->post('id_sd_thread');
		$status = $this->input->post('status');
		//die('approveSD');
		$username = $this->session->userdata('logged_in');
		if($this->session->userdata('level') == 3)
		{
		$data = array(
		'id_sd_thread'=> $idThread,
		'approved_by_konsultan'=> $status,
		'verifier_konsultan'=> $username,
		'status'=> "Approved By ".$username,
		'level'=> $this->session->userdata('level')
		);
		}
		else
		{
		$data = array(
		'id_sd_thread'=> $idThread,
		'approved_by_mp'=> $status,
		'verifier_mp'=> $username,
		'status'=> "Shop Drawing APPROVED",
		'level'=> $this->session->userdata('level')
		);
		}
		//var_dump($data);
		//die();
		$this->Project_model->updateSD($data);
		//die('blablab');
		redirect('project/showThread/'.$idThread);
	}
	else
	{
		redirect('login');
	}
	}
	
	function approveROW()
	{
	if($this->checkLogin() == true)
	{
		$idROW = $this->input->post('idROW');
		$status = $this->input->post('status');
		$idThread = $this->input->post('idThread');
		//die('approveSD');
		$username = $this->session->userdata('logged_in');
		if($this->session->userdata('level') == 3)
		{
		$data = array(
		'id_row'=> $idROW,
		'approved_by_konsultan'=> $status,
		'verifier_konsultan'=> $username,
		'status'=> "Approved By ".$username,
		'level'=> $this->session->userdata('level')
		);
		}
		else
		{
		$data = array(
		'id_row'=> $idROW,
		'approved_by_mp'=> $status,
		'verifier_mp'=> $username,
		'status'=> "Request OF Work APPROVED",
		'level'=> $this->session->userdata('level')
		);
		}
		//var_dump($data);
		//die();
		$this->Project_model->updateROW($data);
		//die('blablab');
		redirect('project/listROW/'.$idThread);
	}
	else
	{
		redirect('login', 'refresh');
	}
	}
	
	function rejectROW()
	{
		if($this->checkLogin() == true)
		{
			echo $idROW = $this->input->post('idROW');
			echo $idThread = $this->input->post('idThread');
			echo $status = $this->input->post('status');
			echo $komentar = $this->input->post('komentar');
			echo $username = $this->session->userdata('logged_in');
			
			if($this->session->userdata('level') == 3)
			{
				$data = array(
				'id_row'=> $idROW,
				'approved_by_konsultan'=> $status,
				'verifier_konsultan'=> $username,
				'status'=> "Rejected By ".$username,
				'level'=> $this->session->userdata('level')
				);
				$this->Project_model->updateROW($data);
			}
			else
			{
				$data = array(
				'id_row'=> $idROW,
				'approved_by_mp'=> $status,
				'verifier_mp'=> $username,
				'status'=> "Rejected By ".$username,
				'level'=> $this->session->userdata('level')
				);
				$this->Project_model->updateROW($data);
			}
			$dataPost = array(
			'row_comment'=>$komentar,
			'username'=>$username,
			'tanggal'=>date('Y-m-d h:i:s'),
			'id_row'=>$idROW
			);
			$this->Project_model->insertPostROW($dataPost);
			
			//die('notApprove');
			redirect('project/listROW/'.$idThread);
		}
		else
		{
			redirect('login');
		}
	}
 
	function addNewThread(){
	if($this->checkLogin() == true)
	{
		//die('addNewThread');
				$config =  array(
                  'upload_path'     => "./assets/file_upload/",
                  'allowed_types'   => "gif|jpg|png|jpeg|pdf",
                  'overwrite'       => TRUE,
                  'max_size'        => "2048000",  // Can be set to particular file size
                  'max_height'      => "1111768",
                  'max_width'       => "1111024"  
                );    
				$this->load->library('upload', $config);
				if($this->upload->do_upload())
				{
					$data = $this->upload->data();
					$file_path     = $data['file_path'];
					$file         = $data['full_path'];
					$file_ext     = $data['file_ext'];
					$final_file_name = "ShopDrawing_".time().$file_ext; 
					rename($file, $file_path . $final_file_name);
					$postThread  = array(
					'subject' => $this->input->post('subject'),
					'kepada' => $this->input->post('kepada'),
					'draw_title' => $this->input->post('draw_title'),
					'draw_number' => $this->input->post('draw_number'),
					'location' => $this->input->post('location'),
					'username' => $this->session->userdata('logged_in'),
					'date' => date('Y-m-d'),
					'id_project_parent' => $this->input->post('projectId'),
					'status' => 'ONGOING'
					);
					$idThread = $this->Project_model->insertThread($postThread);
					$postFile = array(
					'id_sd_thread' => $idThread,
					'nama_file' => $final_file_name
					);
					$insertFile = $this->Project_model->insertFile($postFile);
					redirect('project/showThread/'.$idThread);
				}
	}
	else
	{
		redirect('login', 'refresh');
	}
}

	function saveApproval(){
		if($this->checkLogin() == true)
		{
			//die($idThread);
				$idThread = $this->input->post('id_thread');
				$config =  array(
                  'upload_path'     => "./assets/file_upload/",
                  'allowed_types'   => "gif|jpg|png|jpeg|pdf",
                  'overwrite'       => TRUE,
                  'max_size'        => "2048000",  // Can be set to particular file size
                  'max_height'      => "1111768",
                  'max_width'       => "1111024"  
                );    
				$this->load->library('upload', $config);
				if($this->upload->do_upload())
				{
					$data = $this->upload->data();
					$file_path     = $data['file_path'];
					$file         = $data['full_path'];
					$file_ext     = $data['file_ext'];
					$final_file_name = time().$file_ext; 
					rename($file, $file_path . $final_file_name);
					$dataSD  = array(
					'subject' => $this->input->post('subject'),
					'kepada' => $this->input->post('kepada'),
					'drawing_title' => $this->input->post('draw_title'),
					'drawing_no' => $this->input->post('draw_number'),
					'username' => $this->session->userdata('logged_in'),
					'tanggal' => date('Y-m-d h:i:s'),
					'id_sd_thread' => $this->input->post('id_thread'),
					'file_name' => $final_file_name
					);
					//var_dump($dataSD);
					//die();
					$this->Project_model->insertApproval($dataSD);
					redirect('project/showThread/'.$idThread);
				}
		}
		else
		{
			redirect('login', 'refresh');
		}
	}
 
 
 function showThread($idThread)
 {
	//die('asshole');
	if($this->checkLogin() == true)
	{
		$data['username'] = $this->session->userdata('logged_in');
		$data['level'] = $this->session->userdata('level');
		$data['thread'] = $this->Project_model->getThread($idThread);
		$data['posts'] = $this->Project_model->getPost($idThread);
		$data['files'] = $this->Project_model->getFiles();
		$this->load->view('projects/showThread', $data);
	}
	else
	{
		redirect('login');
	}
 }
 
 function addThread($projectId)
 {
	//die('addThread');
	if($this->checkLogin() == true)
	{
		$data['username'] = $this->session->userdata('logged_in');
		$data['level'] = $this->session->userdata('level');
		$data['projectId'] = $projectId;
		$this->load->view('projects/addThread', $data);
	}
	else
	{
		redirect('login');
	}
 }
 
 function addProject()
 {
	//die('addThread');
	if($this->checkLogin() == true)
	{
		$data['username'] = $this->session->userdata('logged_in');
		$data['level'] = $this->session->userdata('level');
		$this->load->view('projects/addProject', $data);
	}
	else
	{
		redirect('login');
	}
 }
 
 public function addNewProject(){
	if($this->checkLogin() == true)
	{
		//die('addNewProject');
				$config =  array(
                  'upload_path'     => "./assets/file_upload/",
                  'allowed_types'   => "gif|jpg|png|jpeg|pdf",
                  'overwrite'       => TRUE,
                  'max_size'        => "2048000",  // Can be set to particular file size
                  'max_height'      => "1111768",
                  'max_width'       => "1111024"  
                );    
				$this->load->library('upload', $config);
				if($this->upload->do_upload())
				{
					$data = $this->upload->data();
					$file_path     = $data['file_path'];
					$file         = $data['full_path'];
					$file_ext     = $data['file_ext'];
					$final_file_name = "ShopDrawing_".time().$file_ext; 
					rename($file, $file_path . $final_file_name);
					$postProject  = array(
					'nama_project' => $this->input->post('nama_project'),
					'tanggal' => date('Y-m-d')
					);
					$idProject = $this->Project_model->insertProject($postProject);
					$postThread  = array(
					'subject' => $this->input->post('subject'),
					'kepada' => $this->input->post('kepada'),
					'draw_title' => $this->input->post('draw_title'),
					'draw_number' => $this->input->post('draw_number'),
					'location' => $this->input->post('location'),
					'username' => $this->session->userdata('logged_in'),
					'date' => date('Y-m-d'),
					'id_project_parent' => $idProject,
					'status' => 'ONGOING'
					);
					$idThread = $this->Project_model->insertThread($postThread);
					$postFile = array(
					'id_sd_thread' => $idThread,
					'nama_file' => $final_file_name
					);
					$insertFile = $this->Project_model->insertFile($postFile);
					redirect('project/showThread/'.$idThread);
				}
				else
				{
					echo $this->upload->display_errors();
				}
	}
	else
	{
		redirect('login');
	}
}

 
 function checkLogin()
 {
	if($this->session->userdata('logged_in'))
	{
		return true;
	}
	else
	{
		return false;
	}
 }

}
?>